<?php
/**
 * Created by PhpStorm.
 * User: xiangfeng
 * Date: 2022/4/9
 * Time: 12:22 PM
 */

namespace app\api\controller;


use app\api\http\exceptions\ApiErr;
use app\api\http\exceptions\ApiExceptions;
use app\api\service\UserService;
use app\api\validate\UserValidate;
use think\Exception;

class User extends BaseApi
{
    /**
     * 1、查询用户基本信息
     * @return \think\response\Json
     * @throws ApiExceptions
     */
    public function getUserInfo()
    {
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $data = UserService::get($user_id);
        if (isset($data['image_list'])) {
            $data['image_list'] = !empty($data['image_list']) ? json_decode($data['image_list'], true) : [];
        }
        return $this->jsonSuccessResponse($data);
    }

    /**
     * 2、查询服务卡列表信息
     * @return \think\response\Json
     * @throws ApiExceptions
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSerCardList()
    {
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $data = UserService::getSerCardList($user_id);
        return $this->jsonSuccessResponse($data);
    }

    /**
     * 3、查询服务卡列表信息
     * @return \think\response\Json
     * @throws ApiExceptions
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserCardInfo()
    {
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        $data = UserService::getUserCardInfo($user_id);
        return $this->jsonSuccessResponse($data);
    }

    /**
     * 4、修改基本资料
     * @return \think\response\Json
     * @throws ApiExceptions
     */
    public function updateInfo()
    {
        $param = $this->app->request->param();
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        if ($user_id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        try {
            validate(UserValidate::class)->scene('update')->check($param);
        } catch (\Exception $exception) {
            $apiErrConst = ApiErr::UPDATE_DB_FAILED;
            throw new ApiExceptions([$apiErrConst[0], $apiErrConst[1] . ':' . $exception->getMessage()]);
        }
        $param['image_list'] = !empty($param['image_list']) ? json_encode($param['image_list']) : [];
        return $this->jsonSuccessResponse(UserService::update($user_id, $param));
    }

    /**
     * @return \think\response\Json
     * @throws ApiExceptions
     * @throws \think\db\exception\DbException
     */
    public function getColleageUser()
    {
        $param = $this->app->request->param();
        $login_info = $this->login_user;
        $user_id = isset($login_info['user_id']) ? (int)$login_info['user_id'] : 0;
        if ($user_id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        $page = isset($param['cur_page']) ? (int)$param['cur_page'] : 1;
        $limit = isset($param['cur_size']) ? (int)$param['cur_size'] : 20;
        $keywords = isset($param['keywords']) ? $param['keywords'] : '';
        $type = isset($param['type']) ? (int)$param['type'] : 0;
        $page_config = [
            "list_rows" => $limit,
            "page" => $page
        ];
        $data = UserService::getColleageUser($user_id, $page_config, $keywords, $type);
        return $this->jsonSuccessResponse($data);
    }

}